﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightBase
{
	public class Scroll
	{
		/// <summary>
		/// Update scrollbar
		/// </summary>
		public static void SetupScroll()
		{

			((Page)Application.Current.RootVisual).ScrollLayoutRoot.Width = Application.Current.Host.Content.ActualWidth;
			((Page)Application.Current.RootVisual).ScrollLayoutRoot.Height = Application.Current.Host.Content.ActualHeight;

			((Page)Application.Current.RootVisual).Centered_content.MinHeight = 640;
		}


		public class Events
		{

			public static void Content_Resized(object sender, EventArgs e)
			{
				Scroll.SetupScroll();
			}

			public static void ScrollBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
			{
				FrameworkElement item = sender as FrameworkElement;

				item.CaptureMouse();
				item.Cursor = Cursors.Hand;
				item.Tag = Math.Floor(e.GetPosition((Canvas)sender).Y).ToString();
			}

			public static void ScrollBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
			{
				FrameworkElement item = sender as FrameworkElement;
				item.ReleaseMouseCapture();
				item.Cursor = null;
				item.Tag = "";
			}

			public static void ScrollBar_MouseMove(object sender, MouseEventArgs e)
			{
				FrameworkElement item = sender as FrameworkElement;

				Point mousePosition = e.GetPosition((Grid)((Canvas)sender).Parent);
				Point mousePositionOffset = new Point(0, 0);

				double scrollHeight = ((_3party.HtmlTextBlock)((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("MainContentGrid")).Children[0]).ActualHeight;

				if (Convert.ToString(item.Tag) != "")
				{
					mousePositionOffset = new Point(0, Convert.ToDouble(Convert.ToString(item.Tag)));

					int mousePosY = Convert.ToInt32(Math.Floor(mousePosition.Y)) - Convert.ToInt32(Math.Floor(mousePositionOffset.Y));

					if (mousePosY < 0 || mousePosY > 338)
					{
						if (mousePosY <= 0)
							mousePosY = 0;
						else
							mousePosY = 338;
					}

					((_3party.HtmlTextBlock)((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("MainContentGrid")).Children[0]).Margin = new Thickness(12, 36 - (((scrollHeight - 435) / 328) * mousePosY), 10, -420);

					((Canvas)(sender)).Margin = new Thickness(1, mousePosY, 1, 0);
				}
				else
				{
					item.Cursor = Cursors.Hand;
				}
			}
		}
	}
}
